/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.streaming.api.functions.windowing.delta.extractor;

import org.apache.flink.api.java.tuple.Tuple2;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for {@link ConcatenatedExtract}. */
class ConcatenatedExtractTest {

    private String[] testStringArray1 = {"1", "2", "3"};
    private int[] testIntArray1 = {1, 2, 3};
    private String[] testStringArray2 = {"4", "5", "6"};
    private int[] testIntArray2 = {4, 5, 6};
    private String[] testStringArray3 = {"7", "8", "9"};
    private int[] testIntArray3 = {7, 8, 9};
    private Tuple2<String[], int[]>[] testTuple2Array;
    private Tuple2<String[], int[]> testTuple2;
    private Tuple2<Tuple2<String[], int[]>, Tuple2<String[], int[]>[]> testData;

    @SuppressWarnings("unchecked")
    @BeforeEach
    void setupData() {
        testTuple2Array = new Tuple2[2];
        testTuple2Array[0] = new Tuple2<>(testStringArray1, testIntArray2);
        testTuple2Array[1] = new Tuple2<>(testStringArray2, testIntArray1);

        testTuple2 = new Tuple2<>(testStringArray3, testIntArray3);

        testData = new Tuple2<>(testTuple2, testTuple2Array);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Test
    void test1() {
        Extractor ext =
                new ConcatenatedExtract(new FieldFromTuple(0), new FieldFromTuple(1))
                        .add(new FieldsFromArray(Integer.class, 2, 1, 0));
        int[] expected = {testIntArray3[2], testIntArray3[1], testIntArray3[0]};
        assertThat(((Integer[]) ext.extract(testData))[0]).isEqualTo(expected[0]);
        assertThat(((Integer[]) ext.extract(testData))[1]).isEqualTo(expected[1]);
        assertThat(((Integer[]) ext.extract(testData))[2]).isEqualTo(expected[2]);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Test
    void test2() {
        Extractor ext =
                new ConcatenatedExtract(
                                new FieldFromTuple(1), // Tuple2<String[],int[]>[]
                                new FieldsFromArray(Tuple2.class, 1)) // Tuple2<String[],int[]>[]
                        .add(new FieldFromArray(0)) // Tuple2<String[],int[]>
                        .add(new ArrayFromTuple(0)) // Object[] (Containing String[])
                        .add(new FieldFromArray(0)) // String[]
                        .add(new FieldFromArray(1)); // String

        String expected2 = testStringArray2[1];
        assertThat(ext.extract(testData)).isEqualTo(expected2);
    }
}
